package com.insigma.business.qrcode.banner.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.insigma.business.common.utils.ToolsHelper;
import com.insigma.business.qrcode.banner.dao.BannerDAO;
import com.insigma.business.qrcode.banner.dto.BannerDTO;
import com.insigma.business.qrcode.banner.entity.BannerEntity;
import com.insigma.business.qrcode.banner.service.BannerService;
import com.insigma.business.qrcode.common.template.area.BaseProviderImpl;
import com.insigma.business.qrcode.common.template.area.ScenarioCodeEnum;
import com.insigma.business.qrcode.common.template.area.ServiceCallback;
import com.insigma.business.qrcode.common.util.ResponseUtils;
import com.insigma.business.qrcode.common.vo.BaseResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


@Slf4j
@Service("bannerService")
public class BannerServiceImpl extends BaseProviderImpl implements BannerService {


    @Resource
    private BannerDAO bannerDAO;


    /**
     * banner
     * 查询
     * @return
     * @throws Exception
     */
    @Override
    public BaseResponse<List<BannerDTO>> queryAll() throws Exception {


        final BaseResponse<List<BannerDTO>> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(baseResponse, new ServiceCallback() {
            @Override
            public void check() {
            }

            @Override
            public void executeService() {
                QueryWrapper<BannerEntity> fwrapper = new QueryWrapper<>();
                fwrapper.eq("status","10")
                .orderByDesc("order_id");

                List<BannerEntity> list = bannerDAO.selectList(fwrapper);
                List<BannerDTO> dtoList = ToolsHelper.copyListProperties(list, BannerDTO::new);
                dtoList.forEach(i ->{
                    //商家推广前端需要商家详情页面的路由，拼接商家ID 返回
                    if(i.getBannerType().equals("B01")){
                        i.setExternalLinks("/subPackages/storePage/shopDetail?id="+i.getCwmiId());
                    }

                });
                ResponseUtils.setupSuccessResponse(baseResponse,dtoList);
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.CODE_ON_BANNER_QUERY_LIST;
            }
        });
        return baseResponse;
    }
}
